﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Helpers;
using Lead2012.Business;

namespace Lead2012.Repository
{
    public class LeadSourceManagementRepository
    {
        // Create an instance of entities framework
        Lead2012Entities entities = new Lead2012Entities();

        public void GetModel(LeadSourceViewModel model, string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            int counts = 0;
            model.ItemList = GetAllLeadSource(keywords, sortBy, sortOrder, pageIndex, pageSize, ref counts);            

            // Calculate paging
            if (counts > 0)
            {
                model.TotalPage = (counts % pageSize == 0) ? counts / pageSize : (counts / pageSize + 1);
                model.TotalRecord = counts;
                model.PageIndex = pageIndex;
                model.PageSize = pageSize;
            }
        }

        public void GetModel(LeadSourceViewModel model, int id)
        {
            model.LeadSourceInfo = GetSingleBy(id);
        }

        #region #Common
        public List<LeadSourceItem> GetAllLeadSource(string keywords, string sortBy, string sortOrder, int page, int take, ref int counts)
        {
            List<LeadSourceItem> list = new List<LeadSourceItem>();

            // Get milestone / actions
            var items = new LeadSourceRepository().GetAll();

            // Add to list
            foreach (var item in items)
            {
                LeadSourceItem model = new LeadSourceItem();

                model.SourceId = item.SourceId;
                model.Title = item.Title;
                model.Note = item.Note;
                model.Active = item.Active;
                list.Add(model);
            }

            if (!string.IsNullOrEmpty(keywords))
            {
                list = list.Where(c => c.Title.ToLower().Contains(keywords.ToLower())).ToList();
            }

            counts = list.Count;

            
            // Add item to list
            switch (sortBy)
            {
                case "Title":
                    {
                        if (sortOrder == "asc")
                          list =  list.OrderBy(c => c.Title).Skip((page - 1) * take).Take(take).ToList();
                        else
                          list =  list.OrderByDescending(c => c.Title).Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
                default:
                    {
                        list = list.Skip((page - 1) * take).Take(take).ToList();

                        break;
                    }
            }
            
            return list;
        }

        public List<ItemFilter> GetAllMilestone()
        {
            List<ItemFilter> list = new List<ItemFilter>();

            var items = (from c in entities.Milestones select c).ToList();

            foreach (var item in items)
            {
                ItemFilter model = new ItemFilter();
                model.ID = item.Id.ToString();
                model.ItemName = item.Title;
                list.Add(model);
            }

            return list;
        }

        public LeadSourceItem GetSingleBy(int id)
        {
            LeadSourceItem model = new LeadSourceItem();

            var item = (from c in entities.LeadSources where c.SourceId == id select c).FirstOrDefault();
            if (item != null)
            {
                model.SourceId = item.SourceId;
                model.Title = item.Title;
                model.Note = item.Note;
                model.Active = item.Active;
            }

            return model;
        }
        #endregion
    }
}